package com.peng.wx_official_account.controller;

import com.peng.wx_official_account.entity.dto.WeChatPushDto;
import com.peng.wx_official_account.entity.dto.YmlModel;
import com.peng.wx_official_account.entity.enums.WeChatPushTemplateEnum;
import com.peng.wx_official_account.entity.pojo.WxPushUser;
import com.peng.wx_official_account.service.WxPushUserService;
import com.peng.wx_official_account.service.impl.WeChatPushDataService;
import com.peng.wx_official_account.service.impl.WeChatPushImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("wx")
@RequiredArgsConstructor
@SuppressWarnings("SpellCheckingInspection")
public class WeChatController {

    private final YmlModel ymlModel;
    private final WeChatPushImpl service;
    private final WxPushUserService wxPushUserService;
    private final WeChatPushDataService weChatPushDataService;


    /**
     * Check.
     * 微信的校验接口
     *
     * @param request   the request
     * @param response  the response
     * @param signature the signature
     * @param timestamp the timestamp
     * @param nonce     the nonce
     * @param echostr   the echostr
     */
    @GetMapping("check")
    public void check(HttpServletRequest request, HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) {
        log.info("接收到微信的check请求,参数：signature->{}、timestamp->{}、nonce->{}、echostr->{}", signature, timestamp, nonce, echostr);
        service.check(request, response, signature, timestamp, nonce, echostr);
    }

    /**
     * Push test.
     * 公众号测试推送接口
     *
     * @param touser the touser
     * @param str    the str
     */
    @GetMapping("test")
    public void pushTest(String touser, String str) {
        WeChatPushDto data = new WeChatPushDto();
        data.setAppid(ymlModel.getAppId())
                .setTemplate_id(WeChatPushTemplateEnum.TEST_TEMPLATE.getTemplate())
                .setData(weChatPushDataService.testTemplates(str))
                .setTouser(touser);
        service.pushMsdData(data);
    }

    /**
     * Push we chat msg.
     * 微信公众号推送模板
     */
    @GetMapping("push")
    public void pushWeChatMsg() {
        //数据封装在外面完成
        WeChatPushDto weChatPushDto = new WeChatPushDto();
        weChatPushDto.setAppid(ymlModel.getAppId()).setTemplate_id(WeChatPushTemplateEnum.TEMPLATE_WXY.getTemplate());
        weChatPushDto.setData(weChatPushDataService.testTemplates(null));
        for (WxPushUser user : wxPushUserService.lambdaQuery().list()) {
            weChatPushDto.setTouser(user.getOpenId());
            service.pushMsdData(weChatPushDto);
        }
    }

    /**
     * Push data to ljp.
     * <p>
     * 个人专属推送
     *
     * @param type the type  根据类型进行不同的推送  0：LJP 1：WXY 2：CKT
     */
    @GetMapping("ljp")
    public void pushDataToLjp(Integer type) {
        Assert.notNull(type, "参数类型不能为空！");
        WeChatPushDto pushDto = null;

        //消息推送模板填充
        switch (type) {
            case 0:
                pushDto = new WeChatPushDto().setTemplate_id(WeChatPushTemplateEnum.TEMPLATE_LJP.getTemplate())
                        .setAppid(ymlModel.getAppId())
                        .setData(weChatPushDataService.templatesOfLJPAndCKT(0))
                        .setTouser("onlQ562TaIaI0cW1nt1tJiHu-zmQ");
                break;
            case 1:
                pushDto = new WeChatPushDto().setTemplate_id(WeChatPushTemplateEnum.TEMPLATE_WXY_TWO.getTemplate())
                        .setAppid(ymlModel.getAppId())
                        .setData(weChatPushDataService.templatesOfWXYPlus())
                        .setTouser("onlQ56_0RvFKzBtSsW-pjvf1DbIY");
                break;
            case 2:
                pushDto = new WeChatPushDto().setTemplate_id(WeChatPushTemplateEnum.TEMPLATE_LJP.getTemplate())
                        .setAppid(ymlModel.getAppId())
                        .setData(weChatPushDataService.templatesOfLJPAndCKT(1))
                        .setTouser("onlQ56_uLayLBS6YvXvnXbhMb6MA");
                break;
            default:
                break;
        }

        //消息推送
        service.pushMsdData(pushDto);
    }

}
